Why, in Ruby, does Array("foo\nbar") == ["foo\n", "bar"]?
Posted
by Tyson
on Stack Overflow
See other posts from Stack Overflow
or by Tyson
Published on 2010-06-12T08:03:35Z
Indexed on
2010/06/12
8:12 UTC
Read the original article
Hit count: 223
In Ruby 1.8.7, Array("hello\nhello")
gives you ["hello\n", "hello"]
. This does two things that I don't expect:
It splits the string on newlines. I'd expect it simply to give me an array with the string I pass in as its single element without modifying the data I pass in.
Even if you accept that it's reasonable to split a string when passing it to Array, why does it retain the newline character when
"foo\nbar".split
does not?
Additionally:
>> Array.[] "foo\nbar"
=> ["foo\nbar"]
>> Array.[] *"foo\nbar"
=> ["foo\n", "bar"]
© Stack Overflow or respective owner